/**
 ************************************************************************************************
 * 												wavio											*
 ************************************************************************************************
 * 
 * A set of functions for reading and writing Microsoft .WAV files. Wraps stdio.h
 * 
 * Copyright (c) 2014, Aaron L. Jones
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted 
 * provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 
 * conditions and the following disclaimer in the documentation and/or other materials provided 
 * with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
**/

#ifndef __wavio_
#define __wavio_

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdint.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#define WAV_INVALID_STREAM			(0xFFFF)
#define	WAV_DEFAULT_CHUNK_SIZE		(36)
#define	WAV_DEFAULT_SUBCHUNK1_SIZE	(16)
#define	WAV_DEFAULT_AUDIO_FORMAT	(1)
#define	WAV_DEFAULT_SUBCHUNK2_SIZE	(0)

enum
{
	WAV_ERROR_FLAG		= 0x01,
	WAV_WRITE_FLAG		= 0x02,
	WAV_EOF_FLAG		= 0x04
};

/* struct for wav header information */
typedef struct
{
	uint32_t chunk_size;
	uint32_t subchunk1_size;
	uint16_t audio_format;
	uint16_t channelct;
	uint32_t sample_rate;
	uint32_t byte_rate;
	uint16_t block_align;
	uint16_t bit_depth;
	uint32_t subchunk2_size;
} wav_header;

typedef struct
{
	FILE* file;
	wav_header head;
	uint32_t flags;
} wav_file;

typedef struct
{
	uint32_t sample_rate;
	uint16_t bit_depth;
	uint16_t channelct;
} wav_mode;

/**
 * to read: mode = NULL
 * to write: pass mode parameters
**/
extern wav_file* wav_fopen(const char *filename, const wav_mode *mode);

extern int wav_fwrite(const void *ptr, uint32_t framect, wav_file *stream);

extern int wav_fread(void * ptr, uint32_t framect, wav_file *stream);

extern int wav_fclose(wav_file *stream);

extern int wav_feof(wav_file *stream);

extern uint32_t wav_get_sample_rate(wav_file *stream);

extern uint16_t wav_get_bit_depth(wav_file *stream);

extern uint16_t wav_get_channelct(wav_file *stream);

extern uint16_t wav_get_block_align(wav_file *stream);

extern double wav_get_duration(wav_file *stream);

#ifdef __cplusplus
}		// extern "C"
#endif

#endif
